
import java.awt.*;
import java.util.Random;

public class Element extends Rectangle {

    public boolean isActive = false;
    public int[][] elementBlocks;
    public int type;
    public int possition = 1;
    public static int elementsNum = 7;

    public Element() {

        Random rnd = new Random();
        this.type = rnd.nextInt(elementsNum) + 1;

        elementBlocks = getElementBlocks(this.type);
        
        for (int y = 0; y < 2; y++) {
            for (int x = 6; x <= 9; x++) {
                if(Screen.room.block[y][x].id == 1) {
                    Screen.life = false;
                }
            }
        }

        for (int y = 0; y < Screen.room.fieldHeight; y++) {
            boolean cleanRow = true;
            for (int x = 0; x < Screen.room.fieldWidth; x++) {
                if (Room.block[y][x].id == 0) {
                    cleanRow = false;
                }
            }

            if (cleanRow) {
                for (int x = 0; x < Screen.room.fieldWidth; x++) {
                    Room.block[y][x].id = 0;
                }
                Screen.rowsCleaned++;
            }
        }
        for (int y = 1; y < Screen.room.fieldHeight; y++) {
            boolean blankRow = true;
            boolean upperIsBlank = true;
            for (int x = 0; x < Screen.room.fieldWidth; x++) {
                if (Room.block[y][x].id != 0) {
                    blankRow = false;
                }
                if (Room.block[y - 1][x].id != 0) {
                    upperIsBlank = false;
                }
            }

            if (blankRow && !upperIsBlank) {
                for (int x = 0; x < Screen.room.fieldWidth; x++) {
                    for (int i = y; i > 0; i--) {
                        Room.block[i][x].id = Room.block[i - 1][x].id;
                    }
                }
            }
        }
    }

    public void spawnBlock(int x, int y) {
        try {
            setBounds(Screen.room.block[y][x].x, Screen.room.block[y][x].y, 20, 20);
        } catch (Exception e) {
        }
    }

    public int fallFrame = 0, fallSpeed = 2000 - 133 * Screen.level;

    public void physic() {
        Screen.level = 1 + Screen.rowsCleaned / 10;
        fallSpeed = 2000 - 250 * Screen.level;
        if (fallFrame >= fallSpeed) {

            boolean touched = false;

            for (int i = 0; i < 4; i++) {
                if ((elementBlocks[i][1] < 24 && Screen.room.block[elementBlocks[i][1] + 1][elementBlocks[i][0]].id == 1)
                        || elementBlocks[i][1] == 24) {
                    touched = true;
                }
            }

            for (int i = 0; i < 4; i++) {
                if (touched) {
                    Room.block[elementBlocks[i][1]][elementBlocks[i][0]].id = 1;
                    this.isActive = false;
                } else {
                    if (elementBlocks[i][1] < 24) {
                        elementBlocks[i][1]++;
                    }
                }
            }

            fallFrame = 0;
        } else {
            fallFrame++;
        }
    }

    public void draw(Graphics g) {
        g.setColor(new Color(109, 160, 199));
        g.drawRect(x, y, 20, 20);
        g.setColor(new Color(79, 146, 198));
        g.fillRect(x + 1, y + 1, 19, 19);
    }

    public static int[][] getElementBlocks(int id) {

        int[][] element = new int[4][2];

        switch (id) {
            case 1:
                element[0][0] = 8;
                element[0][1] = 0;

                element[1][0] = 9;
                element[1][1] = 0;

                element[2][0] = 8;
                element[2][1] = 1;

                element[3][0] = 9;
                element[3][1] = 1;
                break;
            case 2:
                element[0][0] = 7;
                element[0][1] = 0;

                element[1][0] = 8;
                element[1][1] = 0;

                element[2][0] = 9;
                element[2][1] = 0;

                element[3][0] = 7;
                element[3][1] = 1;
                break;
            case 3:
                element[0][0] = 7;
                element[0][1] = 0;

                element[1][0] = 8;
                element[1][1] = 0;

                element[2][0] = 8;
                element[2][1] = 1;

                element[3][0] = 9;
                element[3][1] = 1;
                break;
            case 4:
                element[0][0] = 6;
                element[0][1] = 0;

                element[1][0] = 7;
                element[1][1] = 0;

                element[2][0] = 8;
                element[2][1] = 0;

                element[3][0] = 9;
                element[3][1] = 0;
                break;
            case 5:
                element[0][0] = 8;
                element[0][1] = 0;

                element[1][0] = 7;
                element[1][1] = 1;

                element[2][0] = 8;
                element[2][1] = 1;

                element[3][0] = 9;
                element[3][1] = 1;
                break;
            case 6:
                element[0][0] = 8;
                element[0][1] = 0;

                element[1][0] = 9;
                element[1][1] = 0;

                element[2][0] = 7;
                element[2][1] = 1;

                element[3][0] = 8;
                element[3][1] = 1;
                break;
            case 7:
                element[0][0] = 7;
                element[0][1] = 0;

                element[1][0] = 7;
                element[1][1] = 1;

                element[2][0] = 8;
                element[2][1] = 1;

                element[3][0] = 9;
                element[3][1] = 1;
                break;
        }

        return element;
    }

    public static int getElementBlockX(int id, int elementID) {
        int[][] elements = getElementBlocks(id);

        return elements[elementID][0];
    }

    public static int getElementBlockY(int id, int elementID) {
        int[][] elements = getElementBlocks(id);

        return elements[elementID][1];
    }
}
